/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.service.error;

import com.wtkj.vo.dzzf.share.staticvalue.CertifiType;
import com.wttech.dzzf.process.dao.DataProcessFinalDao;
import com.wttech.dzzf.process.dao.TransferUserInfoDao;
import com.wttech.dzzf.process.model.user.FundAccount;
import com.wttech.dzzf.process.service.base.BaseInfoService;
import com.wttech.dzzf.process.util.DataValidationCheck;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

/**
 * @author MaShibo
 * @date 2017/12/2.
 */
@Service
public class DataProcessFinalService {

	private static final Logger LOGGER = LoggerFactory.getLogger(DataProcessFinalService.class);

	private static final int TAXPAYERCODE = 102;

	@Resource
	private TransferUserInfoDao transferUserInfoDao;

	@Resource
	private DataProcessFinalDao dataProcessFinalDao;

	@Resource
	private BaseInfoService baseInfoService;

	@Resource
	private ThreadPoolTaskExecutor executor;

	private void recordPersonErrorOwnerTypeInfos(){
		List<FundAccount> fundAccounts = dataProcessFinalDao.queryPersonErrorOwnerTypeInfos();
		LOGGER.info("共【{}】条待验证数据", fundAccounts.size());
		List<Future<Void>> futures = new ArrayList<>(fundAccounts.size());
		fundAccounts.forEach(fundAccount -> futures.add(executor.submit(() -> {
			checkPersonErrorOwnerFundAccount(fundAccount);
			return null;
		})));
		baseInfoService.getFutureResult(futures);
		LOGGER.info("执行结束");
		executor.shutdown();
	}

	private void checkPersonErrorOwnerFundAccount(FundAccount fundAccount){
		String certifiNo = fundAccount.getCompanyLic();
		if(!DataValidationCheck.checkCertifiNoOne(certifiNo)){
			transferUserInfoDao.createTestObjcet(fundAccount);
			LOGGER.info("错误信息已记录");
		}else {
			Integer certifiType = fundAccount.getCertifiType();
			if (certifiType != CertifiType.ORGCODE && certifiType != TAXPAYERCODE){
				LOGGER.info("资金账户【{}】，证件号【{}】，证件类型【{}】错误", new Object[]{fundAccount.getFundAccountId(), certifiNo, certifiType});
			}
		}
	}

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		DataProcessFinalService service = context.getBean(DataProcessFinalService.class);
		service.recordPersonErrorOwnerTypeInfos();
	}
}
